package com.wangxiang.crawler.controller;



import com.wangxiang.crawler.dao.ClothingSkuDao;
import com.wangxiang.crawler.dao.ClothingSpuDao;
import com.wangxiang.crawler.dao.PhoneSkuDao;
import com.wangxiang.crawler.dao.PhoneSpuDao;
import com.wangxiang.crawler.entity.ClothingSkuEntity;
import com.wangxiang.crawler.entity.ClothingSpuEntity;
import com.wangxiang.crawler.entity.PhoneSkuEntity;
import com.wangxiang.crawler.entity.PhoneSpuEntity;
import com.wangxiang.crawler.utils.HttpClientUtil;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;



import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

@RestController
@RequestMapping("crawler")
public class CrawlerController {
    @Autowired
    private HttpClientUtil httpClientUtil;
    @Autowired
    private ClothingSpuDao clothingSpuDao;
    @Autowired
    private ClothingSkuDao clothingSkuDao;
    @Autowired
    private PhoneSkuDao phoneSkuDao;
    @Autowired
    private PhoneSpuDao phoneSpuDao;

    @RequestMapping("JD")
    public void JD(@RequestBody HashMap<String , String> map) throws IOException, InterruptedException {

        String type = map.get("type");
        //100页
        for (int i = 1; i < 199; i=i+2) {
            String http = httpClientUtil.http(map.get("url") + i);
            //解析
            Document parse = Jsoup.parse(http);
            Element spuRoot = parse.getElementById("J_goodsList");
            if (spuRoot!=null){
                Elements spu = spuRoot.getElementsByClass("gl-i-wrap");
                spu.forEach((spuItem)->{
                    ClothingSpuEntity clothingSpuEntity = new ClothingSpuEntity();
                    //先拿地址
                    String url = spuItem.select("div.p-img>a").attr("href");
                    //拿图片
                    String src = spuItem.select("div.p-img>a>img").attr("data-lazy-img");
                    clothingSpuEntity.setImage("http:"+src);
                    //类型
                    clothingSpuEntity.setType(type);

                    //标题
                    String title = spuItem.getElementsByClass("p-name p-name-type-2").text();
                    clothingSpuEntity.setTitle(title);


                    //价格
                    String price = spuItem.select("div.p-price>strong>i").text();
                    clothingSpuEntity.setPrice(Double.valueOf(price));
                    //
                    ArrayList<String> sizeListSpu = new ArrayList<>();
                    ArrayList<String> colorListSpu = new ArrayList<>();

                    HashMap<String, Object> paramMap = new HashMap<>();
                    //子页面
                    try {
                        String childrenHtml = httpClientUtil.http("https:" + url);
                        //解析二级页面
                        if (url!=null){
                            Document skuParse = Jsoup.parse(childrenHtml);
                            Elements elementsByClass = skuParse.getElementsByClass("itemInfo-wrap");
                            //所有参数
                            //所有参数，使用map来存储
                            Elements skuLi = skuParse.getElementsByClass("parameter2 p-parameter-list").select("li");

                            skuLi.forEach((liItem)->{
                                paramMap.put(StringUtils.substringBefore(liItem.text(),"："),StringUtils.substringAfter(liItem.text(),"："));
                            });
                            //获取品牌
                            Element brandRoot = skuParse.getElementById("parameter-brand");
                            if (brandRoot!=null){
                                String brand = brandRoot.getElementsByTag("li").attr("title");
                                clothingSpuEntity.setBrand(brand);
                            }
                            //颜色，尺寸
                            Element colors = skuParse.getElementById("choose-attr-1");
                            Element sizes = skuParse.getElementById("choose-attr-2");

                            //颜色集合
                            if(colors!=null){
                                if (colors.attr("data-type").equals("颜色")){
                                    Elements colorList = colors.getElementsByClass("item");
                                    colorList.forEach((colorItem)->{
                                        String color = colorItem.attr("data-value");
                                        if (color!=null){
                                            System.out.println("颜色"+color);
                                            colorListSpu.add(color);
                                        }
                                    });
                                }

                                clothingSpuEntity.setColorList(colorListSpu.toString());
                            }
                            if (sizes!=null){
                                if (sizes.attr("data-type").equals("尺码")){
                                    Elements sizeList = sizes.getElementsByClass("item");
                                    sizeList.forEach((sizeItem)->{
                                        String size = sizeItem.attr("data-value");
                                        if (size!=null){
                                            sizeListSpu.add(size);
                                        }
                                    });
                                }
                                clothingSpuEntity.setSizeList(sizeListSpu.toString());
                            }

                            //所以参数map
                            if (paramMap!=null){
                                clothingSpuEntity.setParamMap(paramMap.toString());
                            }
                            //插入spu
                            int insert = clothingSpuDao.insertGetKey(clothingSpuEntity);
                            if (insert>0){
                                //这里开始sku的插入
                                //颜色尺寸都不为空
                                ClothingSkuEntity clothingSkuEntity = new ClothingSkuEntity();
                                if (colors!=null & sizes!=null){
                                    Elements colorList = colors.getElementsByClass("item");
                                    colorList.forEach((colorItem)->{
                                        String color = colorItem.attr("data-value");
                                        //颜色
                                        clothingSkuEntity.setColor(color);
                                        //品牌
                                        clothingSkuEntity.setBrand(clothingSpuEntity.getBrand());
                                        //类型
                                        clothingSkuEntity.setType(type);
                                        //spu_id 父级id
                                        clothingSkuEntity.setSpuId(clothingSpuEntity.getId());
                                        //颜色对应的图片
                                        Elements skuImg = colorItem.getElementsByTag("img");
                                        if (skuImg!=null){
                                            String imgurl = skuImg.attr("src");
                                            clothingSkuEntity.setImg("http:"+imgurl);
                                        }
                                        Elements sizeList = sizes.getElementsByClass("item");
                                        sizeList.forEach((sizeItem)->{
                                            String size = sizeItem.attr("data-value");
                                            clothingSkuEntity.setSize(size);
                                            int insert1 = clothingSkuDao.insert(clothingSkuEntity);
                                            if (insert1>0){
                                                System.out.println("有颜色有尺寸的"+clothingSpuEntity.getBrand()+type+"爬取成功");
                                            }
                                            try {
                                                Thread.sleep(1*10);
                                            } catch (InterruptedException e) {
                                                e.printStackTrace();
                                            }
                                        });
                                    });
                                }

                                //有颜色没尺寸
                                if (colors!=null & sizes==null){
                                    Elements colorList = colors.getElementsByClass("item");

                                    //品牌
                                    clothingSkuEntity.setBrand(clothingSpuEntity.getBrand());
                                    //类型
                                    clothingSkuEntity.setType(type);
                                    //spu_id 父级id
                                    clothingSkuEntity.setSpuId(clothingSpuEntity.getId());
                                    colorList.forEach((colorItem)->{
                                        String color = colorItem.attr("data-value");
                                        //颜色
                                        clothingSkuEntity.setColor(color);
                                        //颜色对应的图片
                                        Elements skuImg = colorItem.getElementsByTag("img");
                                        if (skuImg!=null){
                                            String imgurl = skuImg.attr("src");
                                            clothingSkuEntity.setImg("http:"+imgurl);
                                        }
                                        int insert1 = clothingSkuDao.insert(clothingSkuEntity);
                                        if (insert1>0){
                                            System.out.println("有颜色没尺寸的"+clothingSpuEntity.getBrand()+type+"爬取成功");
                                        }
                                        try {
                                            Thread.sleep(1*10);
                                        } catch (InterruptedException e) {
                                            e.printStackTrace();
                                        }
                                    });
                                }
                                //没颜色有尺寸
                                if (colors==null & sizes!=null){
                                    Elements sizeList = sizes.getElementsByClass("item");
                                    //品牌
                                    clothingSkuEntity.setBrand(clothingSpuEntity.getBrand());
                                    //类型
                                    clothingSkuEntity.setType(type);
                                    //spu_id 父级id
                                    clothingSkuEntity.setSpuId(clothingSpuEntity.getId());
                                    sizeList.forEach((sizeItem)->{
                                        String size = sizeItem.attr("data-value");
                                        clothingSkuEntity.setSize(size);
                                        int insert1 = clothingSkuDao.insert(clothingSkuEntity);
                                        if (insert1>0){
                                            System.out.println("没颜色有尺寸的"+clothingSpuEntity.getBrand()+type+"爬取成功");
                                        }
                                        try {
                                            Thread.sleep(1*10);
                                        } catch (InterruptedException e) {
                                            e.printStackTrace();
                                        }
                                    });
                                }
                            }

                        }
                        System.out.println("实体"+clothingSpuEntity);
                    Thread.sleep(1*100);

                    } catch (IOException | InterruptedException e) {
                        e.printStackTrace();
                    }
                });
            }
            Thread.sleep(3*10);
        }
    }
    @RequestMapping("mobile")
    public void phone(@RequestBody HashMap<String , String> map) throws IOException, InterruptedException {
        String type = map.get("type");
        //100页
        for (int i = 1; i < 100; i=i+2) {
            String http = httpClientUtil.http(map.get("url") + i);
            //解析
            Document parse = Jsoup.parse(http);
            Element spuRoot = parse.getElementById("J_goodsList");
            if (spuRoot!=null){
                Elements spu = spuRoot.getElementsByClass("gl-i-wrap");
                spu.forEach((spuItem)->{
//                    ClothingSpuEntity clothingSpuEntity = new ClothingSpuEntity();
                    PhoneSpuEntity phoneSpuEntity = new PhoneSpuEntity();
                    //先拿地址
                    String url = spuItem.select("div.p-img>a").attr("href");
                    //拿图片
                    String src = spuItem.select("div.p-img>a>img").attr("data-lazy-img");
                    phoneSpuEntity.setImage("http:"+src);
                    //类型
                    phoneSpuEntity.setType(type);

                    //标题
                    String title = spuItem.getElementsByClass("p-name p-name-type-2").text();
                    phoneSpuEntity.setTitle(title);


                    //价格
                    String price = spuItem.select("div.p-price>strong>i").text();
                    phoneSpuEntity.setPrice(Double.valueOf(price));
                    //
                    ArrayList<String> sizeListSpu = new ArrayList<>();
                    ArrayList<String> colorListSpu = new ArrayList<>();

                    HashMap<String, Object> paramMap = new HashMap<>();
                    //子页面
                    try {
                        String childrenHtml = httpClientUtil.http("https:" + url);
                        //解析二级页面
                        if (url!=null){
                            Document skuParse = Jsoup.parse(childrenHtml);
                            Elements elementsByClass = skuParse.getElementsByClass("itemInfo-wrap");
                            //所有参数
                            //所有参数，使用map来存储
                            Elements skuLi = skuParse.getElementsByClass("parameter2 p-parameter-list").select("li");

                            skuLi.forEach((liItem)->{
                                paramMap.put(StringUtils.substringBefore(liItem.text(),"："),StringUtils.substringAfter(liItem.text(),"："));
                            });
                            //获取品牌
                            Element brandRoot = skuParse.getElementById("parameter-brand");
                            if (brandRoot!=null){
                                String brand = brandRoot.getElementsByTag("li").attr("title");
                                phoneSpuEntity.setBrand(brand);
                            }
                            //颜色，尺寸
                            Element colors = skuParse.getElementById("choose-attr-1");
                            Element sizes = skuParse.getElementById("choose-attr-2");

                            //颜色集合
                            if(colors!=null){

                                    Elements colorList = colors.getElementsByClass("item");
                                    colorList.forEach((colorItem)->{
                                        String color = colorItem.attr("data-value");
                                        if (color!=null){
                                            System.out.println("颜色"+color);

                                            colorListSpu.add(color);
                                        }
                                    });


                                phoneSpuEntity.setColorList(colorListSpu.toString());
                            }
                            if (sizes!=null){
                                    Elements sizeList = sizes.getElementsByClass("item");
                                    sizeList.forEach((sizeItem)->{
                                        String size = sizeItem.attr("data-value");
                                        if (size!=null){
                                            sizeListSpu.add(size);
                                            System.out.println("内存"+size);
                                        }
                                    });
                                phoneSpuEntity.setRamList(sizeListSpu.toString());
                            }

                            //所以参数map
                            if (paramMap!=null){
                                phoneSpuEntity.setParamMap(paramMap.toString());
                            }
                            //插入spu
                            int insert = phoneSpuDao.insertGetKeyPhone(phoneSpuEntity);
                            if (insert>0){
                                //这里开始sku的插入
                                //颜色尺寸都不为空
//                                ClothingSkuEntity clothingSkuEntity = new ClothingSkuEntity();
                                PhoneSkuEntity phoneSkuEntity = new PhoneSkuEntity();
                                if (colors!=null & sizes!=null){
                                    Elements colorList = colors.getElementsByClass("item");
                                    colorList.forEach((colorItem)->{
                                        String color = colorItem.attr("data-value");
                                        //颜色
                                        phoneSkuEntity.setColor(color);
                                        //品牌
                                        phoneSkuEntity.setBrand(phoneSpuEntity.getBrand());
                                        //类型
                                        phoneSkuEntity.setType(type);
                                        //spu_id 父级id
                                        phoneSkuEntity.setSpuId(phoneSpuEntity.getId());
                                        Elements skuImg = colorItem.getElementsByTag("img");
                                        if (skuImg!=null){
                                            String imgurl = skuImg.attr("src");
                                            phoneSkuEntity.setImg("http:"+imgurl);
                                        }
                                        Elements sizeList = sizes.getElementsByClass("item");
                                        sizeList.forEach((sizeItem)->{
                                            String size = sizeItem.attr("data-value");
                                            phoneSkuEntity.setRam(size);
                                            int insert1 = phoneSkuDao.insert(phoneSkuEntity);
                                            if (insert1>0){
                                                System.out.println("有颜色有尺寸的"+phoneSpuEntity.getBrand()+type+"爬取成功");
                                            }
                                            try {
                                                Thread.sleep(1*10);
                                            } catch (InterruptedException e) {
                                                e.printStackTrace();
                                            }
                                        });
                                    });
                                }

                                //有颜色没尺寸
                                if (colors!=null & sizes==null){
                                    Elements colorList = colors.getElementsByClass("item");

                                    //品牌
                                    phoneSkuEntity.setBrand(phoneSpuEntity.getBrand());
                                    //类型
                                    phoneSkuEntity.setType(type);
                                    //spu_id 父级id
                                    phoneSkuEntity.setSpuId(phoneSpuEntity.getId());
                                    colorList.forEach((colorItem)->{
                                        String color = colorItem.attr("data-value");
                                        //颜色
                                        phoneSkuEntity.setColor(color);
                                        Elements skuImg = colorItem.getElementsByTag("img");
                                        if (skuImg!=null){
                                            String imgurl = skuImg.attr("src");
                                            phoneSkuEntity.setImg("http:"+imgurl);
                                        }
                                        int insert1 = phoneSkuDao.insert(phoneSkuEntity);
                                        if (insert1>0){
                                            System.out.println("有颜色没尺寸的"+phoneSpuEntity.getBrand()+type+"爬取成功");
                                        }
                                        try {
                                            Thread.sleep(1*10);
                                        } catch (InterruptedException e) {
                                            e.printStackTrace();
                                        }
                                    });
                                }
                                //没颜色有尺寸
                                if (colors==null & sizes!=null){
                                    Elements sizeList = sizes.getElementsByClass("item");
                                    //品牌
                                    phoneSkuEntity.setBrand(phoneSpuEntity.getBrand());
                                    //类型
                                    phoneSkuEntity.setType(type);
                                    //spu_id 父级id
                                    phoneSkuEntity.setSpuId(phoneSpuEntity.getId());
                                    sizeList.forEach((sizeItem)->{
                                        String size = sizeItem.attr("data-value");
                                        phoneSkuEntity.setRam(size);
                                        int insert1 = phoneSkuDao.insert(phoneSkuEntity);
                                        if (insert1>0){
                                            System.out.println("没颜色有尺寸的"+phoneSpuEntity.getBrand()+type+"爬取成功");
                                        }
                                        try {
                                            Thread.sleep(1*10);
                                        } catch (InterruptedException e) {
                                            e.printStackTrace();
                                        }
                                    });
                                }
                            }

                        }
                        System.out.println("实体"+phoneSpuEntity);
                        Thread.sleep(3*100);

                    } catch (IOException | InterruptedException e) {
                        e.printStackTrace();
                    }
                });
            }
            Thread.sleep(3*10);
        }
    }
}
